<?php
interface IErrorHandler {
	public function doError();
}

interface IHttpHandler {
	public function httpDo();
}

interface IHttpListener {
	public function onRequestReceived($params);
}

interface IHttpOutputable {
	public function output();
}

class HTTP {
	const GET  = 0;
	const POST = 1;
}

class HttpAction {
	const GET_CALENDAR  = 1;
	const POST_CALENDAR = 2;
}

class HttpError {
	const HTTP_OK					= 0;
	// Calendar service error
	const HTTP_DUPLICATE_CALENDAR	= -1;
	const HTTP_CALENDAR_SAVE_FAILED	= -2;
	
	private $err_code;
	private $err_msg;
	
	public function __construct($code, $msg) {
		$this->err_code = $code;
		$this->err_msg = $msg;
	}
	
	public function getErrorCode() {return $this->err_code;}
	public function getErrorMsg() {return $this->err_msg;}
}

class HttpStandardErrorHandler implements IErrorHandler {
	private $http_err;
	
	public function setError($e) {$this->http_err = $e;}
	
	public function doError() {
		
		if ($this->http_err instanceof HttpError) {
			$err = $this->http_err->getErrorCode();
			switch($err) {
				case HttpError::HTTP_DUPLICATE_CALENDAR:
					//header("HTTP/1.1 409 CONFLICT");
					HttpResponse::Respond(409);
				case HttpError::HTTP_CALENDAR_SAVE_FAILED:
					//header("HTTP/1.1 500 INTERNAL SERVER ERROR");
					HttpResponse::Respond(500);
				case HttpError::HTTP_OK:
					//header("HTTP/1.1 201 CREATED");
					HttpResponse::Respond(201);
				default:
					//header("HTTP/1.1 200 OK");
					HttpResponse::Respond(200);
			}
		}
	}
}

class HttpResponse {
	public static function Respond($code, $msg = null) {
		$resp = '';
		if ($code != null)
			$resp .= "CODE\t".$code;
			
		if ($msg != null) {
			if ($code != null)
				$resp .= "\n";
				
			$resp .= "MESSAGE\t".$msg;
		}
		echo $resp;
	}
}
?>